package xyz.syyrjx.blog.scheduling;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import xyz.syyrjx.blog.dao.ai.AiIdentityDao;
import xyz.syyrjx.blog.dao.ai.AiIdentityGradeDao;
import xyz.syyrjx.blog.entity.ai.AiIdentity;
import xyz.syyrjx.blog.entity.ai.AiIdentityGrade;
import xyz.syyrjx.blog.util.CachePool;
import xyz.syyrjx.blog.util.ConstValuePool;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 大地崩坏苍蝇兽
 * @date 2024/2/22 11:16
 * @description 过期ai身份清除类
 * @since v0.0.1
 */
@Component
public class ExpiryAiIdentityClear {

    private final Logger logger = LoggerFactory.getLogger(ExpiryAiIdentityClear.class);

    @Resource
    private AiIdentityDao aiIdentityDao;

    @Resource
    private AiIdentityGradeDao aiIdentityGradeDao;

//    @Scheduled(cron ="0/30 * * * * ?")
    @Scheduled(cron ="0 0 0/2 * * ?")
    public void doTask(){
        List<AiIdentity> aiIdentities = aiIdentityDao.selectExpiredAiIdentities(ConstValuePool.COOKIE_MAX_AGE);
        for (AiIdentity aiIdentity : aiIdentities) {
            logger.info("删除身份信息：{}", aiIdentity);
            CachePool.clearAiIdentity(aiIdentity.getIdentity());
            aiIdentityDao.deleteIdentityById(aiIdentity.getId());
            aiIdentityGradeDao.deleteIdentityByIdentityId(aiIdentity.getId());
        }
    }
}
